package com.iretailer.report.analyze.flow;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iretailer.report.analyze.annotation.AnalyzeClass;
import com.iretailer.report.analyze.commonModel.InOutMetric;
import com.iretailer.report.analyze.commonModel.PassengerFlowModel;
import com.iretailer.report.analyze.commonModel.QueueModel;
import com.iretailer.report.analyze.commonModel.ServiceModel;
import com.iretailer.report.analyze.commonUtil.Xml2JSONUtil;
import com.iretailer.report.analyze.flow.Xml2Data;
import com.iretailer.report.util.TimeUtil;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/3/9 21:46
 * @Description:
 */
public class CommonFunc {

    public static PassengerFlowModel transformation(String context, Xml2Data xml2Data) {
        JSONObject object = Xml2JSONUtil.documentToJSONObject(context);
        PassengerFlowModel flow = new PassengerFlowModel();
        flow.setDeviceId((String) object.get("DeviceId"));
        flow.setSiteId((String) object.get("SiteId"));
        JSONArray properties = (JSONArray) object.get("Properties");
        if (properties != null && properties.size() > 0) {
            Map<String, String> prop = (Map) properties.get(0);
            flow.setIpAddress(prop.get("IpAddress"));
            flow.setMacAddress(prop.get("MacAddress"));
            flow.setSerialNumber(prop.get("SerialNumber"));
            JSONArray reportDatas = (JSONArray) object.get("ReportData");
            if (reportDatas != null && reportDatas.size() > 0) {
                for (int i = 0, rdlen = reportDatas.size(); i < rdlen; i++) {
                    Map reportData = (Map) reportDatas.get(i);
                    JSONArray reports = (JSONArray) reportData.get("Report");
                    if (reports != null && reports.size() > 0) {
                        xml2Data.deviceTypeDiff(object, flow);
//                        switchFunc(reports, flow);
                        xml2Data.dataDiff(reports, flow);
                    }
                }
            }
        }
        return flow;
    }

    public static void switchFunc(JSONArray reports, PassengerFlowModel flow) {

        List<InOutMetric> inOutMetrics = new ArrayList<>();
        List<ServiceModel> serviceModels = new ArrayList<>();
        List<QueueModel> queueModels = new ArrayList<>();

        for (int r = 0, rlen = reports.size(); r < rlen; r++) {
            Map report = (Map) reports.get(r);
//            String date = (String) report.get("Date");
            LocalDate date = TimeUtil.parse_LocalDate((String) report.get("Date"));
            JSONArray objects = (JSONArray) report.get("Object");

            if (objects != null && objects.size() > 0) {
                for (int o = 0, olen = objects.size(); o < olen; o++) {
                    Map object = (Map) objects.get(o);
                    String objectType = (String) object.get("ObjectType");
                    flow.setDeviceId((String) object.get("DeviceId"));
                    flow.setDeviceName((String) object.get("DeviceName"));
                    String zoneId = (String) object.get("Id");
                    String zoneName = (String) object.get("Name");
                    switch (objectType) {
                        //客流 进出
                        case "0":
                            JSONArray counts = (JSONArray) object.get("Count");
                            if (counts != null && counts.size() > 0) {
                                for (int c = 0, clen = counts.size(); c < clen; c++) {
                                    Map count = (Map) counts.get(c);
                                    InOutMetric inout = new InOutMetric();
                                    inout.setZone(zoneId);
//                                    inout.setZoneName(zoneName);
                                    inout.setDate(date);
                                    inout.setStartTime(TimeUtil.parse_LocalTime((String) count.get("StartTime")));
                                    inout.setEndTime(TimeUtil.parse_LocalTime((String) count.get("EndTime")));
                                    inout.setEnter(Integer.parseInt((String) count.get("Enters")));
                                    inout.setExit(Integer.parseInt((String) count.get("Exits")));
                                    inOutMetrics.add(inout);
                                }
                            }

                            break;
                        //  排队
                        case "1":
                            JSONArray queues = (JSONArray) object.get("Queue");
                            if (queues != null && queues.size() > 0) {
                                for (int q = 0, qlen = queues.size(); q < qlen; q++) {
                                    Map queue = (Map) queues.get(q);
                                    QueueModel queueModel = new QueueModel();
                                    queueModel.setZone(zoneId);
//                                    queueModel.setZoneName(zoneName);
                                    queueModel.setDate(date);
                                    queueModel.setStartTime(TimeUtil.parse_LocalTime((String) queue.get("StartTime")));
                                    queueModel.setEndTime(TimeUtil.parse_LocalTime((String) queue.get("EndTime")));
                                    queueModel.setAvgLen(Double.parseDouble((String) queue.get("AvgQLength")));
                                    queueModel.setMaxLen(Integer.parseInt((String) queue.get("MaxQLength")));
                                    queueModel.setMinLen(Integer.parseInt((String) queue.get("MinQLength")));
                                    queueModels.add(queueModel);
                                }
                            }

                            break;
                        // 停留
                        case "4":
                            JSONArray services = (JSONArray) object.get("Service");
                            if (services != null && services.size() > 0) {
                                for (int s = 0, slen = services.size(); s < slen; s++) {
                                    Map service = (Map) services.get(s);
                                    ServiceModel serviceModel = new ServiceModel();
                                    serviceModel.setZone(zoneId);
//                                    serviceModel.setZoneName(zoneName);
                                    serviceModel.setDate(date);
                                    serviceModel.setStartTime(TimeUtil.parse_LocalTime((String) service.get("StartTime")));
                                    serviceModel.setEndTime(TimeUtil.parse_LocalTime((String) service.get("EndTime")));
                                    serviceModel.setNumberServed(Integer.parseInt((String) service.get("NumberServed")));
                                    serviceModel.setSecondsOccupied(Integer.parseInt((String) service.get("SecondsOccupied")));
                                    serviceModels.add(serviceModel);
                                }
                            }

                            break;
                    }
                }
            }
        }
        flow.setInOutMetrics(inOutMetrics);
        flow.setServiceModels(serviceModels);
        flow.setQueueModels(queueModels);

    }

    public static void switchFuncIrc1600(JSONArray reports, PassengerFlowModel flow) {
        List<InOutMetric> inOutMetrics = new ArrayList<>();
        List<ServiceModel> serviceModels = new ArrayList<>();
        List<QueueModel> queueModels = new ArrayList<>();
        for (int r = 0, rlen = reports.size(); r < rlen; r++) {
            Map report = (Map) reports.get(r);
//            String date = (String) report.get("Date");
            LocalDate date = TimeUtil.parse_LocalDate((String) report.get("Date"));
            JSONArray objects = (JSONArray) report.get("Object");

            if (objects != null && objects.size() > 0) {
                for (int o = 0, olen = objects.size(); o < olen; o++) {
                    Map object = (Map) objects.get(o);
                    String objectType = (String) object.get("ObjectType");
                    flow.setDeviceId((String) object.get("DeviceId"));
                    flow.setDeviceName((String) object.get("DeviceName"));
                    String zoneId = (String) object.get("Id");
                    String zoneName = (String) object.get("Name");
                    switch (objectType) {
                        //客流 进出
                        case "0":
                            JSONArray counts = (JSONArray) object.get("Count");
                            if (counts != null && counts.size() > 0) {
                                for (int c = 0, clen = counts.size(); c < clen; c++) {
                                    Map count = (Map) counts.get(c);
                                    InOutMetric inout = new InOutMetric();
                                    int enter = Integer.parseInt((String) count.get("Enters"));
                                    int exit = Integer.parseInt((String) count.get("Exits"));
                                    inout.setZone(zoneId);
//                                    inout.setZoneName(zoneName);
                                    inout.setDate(date);
                                    inout.setStartTime(TimeUtil.parse_LocalTime((String) count.get("StartTime")));
                                    inout.setEndTime(TimeUtil.parse_LocalTime((String) count.get("EndTime")));
                                    switch (zoneId) {
                                        case "6":
                                        case "7":
                                            enter = enter + exit;
                                            exit = 0;
                                            break;
                                    }
                                    inout.setEnter(enter);
                                    inout.setExit(exit);
                                    inOutMetrics.add(inout);
                                }
                            }

                            break;
                        // 停留
                        case "4":
                            JSONArray services = (JSONArray) object.get("Service");
                            if (services != null && services.size() > 0) {
                                for (int s = 0, slen = services.size(); s < slen; s++) {
                                    Map service = (Map) services.get(s);
                                    ServiceModel serviceModel = new ServiceModel();
                                    serviceModel.setZone(zoneId);
//                                    serviceModel.setZoneName(zoneName);
                                    serviceModel.setDate(date);
                                    serviceModel.setStartTime(TimeUtil.parse_LocalTime((String) service.get("StartTime")));
                                    serviceModel.setEndTime(TimeUtil.parse_LocalTime((String) service.get("EndTime")));
                                    serviceModel.setNumberServed(Integer.parseInt((String) service.get("NumberServed")));
                                    serviceModel.setSecondsOccupied(Integer.parseInt((String) service.get("SecondsOccupied")));
                                    serviceModels.add(serviceModel);
                                }
                            }

                            break;
                    }
                }
            }
        }
        flow.setInOutMetrics(inOutMetrics);
        flow.setServiceModels(serviceModels);
        flow.setQueueModels(queueModels);
    }

    public static void switchFuncNec(JSONArray reports, PassengerFlowModel flow) {

        List<InOutMetric> inOutMetrics = new ArrayList<>();
        List<ServiceModel> serviceModels = new ArrayList<>();
        List<QueueModel> queueModels = new ArrayList<>();

        for (int r = 0, rlen = reports.size(); r < rlen; r++) {
            Map report = (Map) reports.get(r);
//            String date = (String) report.get("Date");
            LocalDate date = TimeUtil.parse_LocalDate((String) report.get("Date"));
            JSONArray counts = (JSONArray) report.get("Count");

            if (counts != null && counts.size() > 0) {
                for (int o = 0, olen = counts.size(); o < olen; o++) {
                    Map count = (Map) counts.get(o);
                    InOutMetric inout = new InOutMetric();
                    String zoneId = (String) count.get("Id");
                    //只有个是 zoneId 为 0 才入库
                    if (zoneId.equals("0")) {
                        inout.setZone(zoneId);
//                        inout.setZoneName("");
                        inout.setDate(date);
                        inout.setStartTime(TimeUtil.parse_LocalTime((String) count.get("StartTime")));
                        inout.setEndTime(TimeUtil.parse_LocalTime((String) count.get("EndTime")));
                        inout.setEnter(Integer.parseInt((String) count.get("Enters")));
                        inout.setExit(Integer.parseInt((String) count.get("Exits")));
                        inOutMetrics.add(inout);
                    }
                }

            }
        }
        flow.setInOutMetrics(inOutMetrics);
        flow.setServiceModels(serviceModels);
        flow.setQueueModels(queueModels);

    }


    public static void switchFuncIrc1200(JSONArray reports, PassengerFlowModel flow) {

        List<InOutMetric> inOutMetrics = new ArrayList<>();
        List<ServiceModel> serviceModels = new ArrayList<>();
        List<QueueModel> queueModels = new ArrayList<>();

        for (int r = 0, rlen = reports.size(); r < rlen; r++) {
            Map report = (Map) reports.get(r);
//            String date = (String) report.get("Date");
            LocalDate date = TimeUtil.parse_LocalDate((String) report.get("Date"));
            JSONArray counts = (JSONArray) report.get("Count");

            if (counts != null && counts.size() > 0) {
                for (int o = 0, olen = counts.size(); o < olen; o++) {
                    Map count = (Map) counts.get(o);
                    InOutMetric inout = new InOutMetric();
                    inout.setZone("0");
//                    inout.setZoneName("");
                    inout.setDate(date);
                    inout.setStartTime(TimeUtil.parse_LocalTime((String) count.get("StartTime")));
                    inout.setEndTime(TimeUtil.parse_LocalTime((String) count.get("EndTime")));
                    inout.setEnter(Integer.parseInt((String) count.get("Enters")));
                    inout.setExit(Integer.parseInt((String) count.get("Exits")));
                    inOutMetrics.add(inout);
                }

            }
        }
        flow.setInOutMetrics(inOutMetrics);
        flow.setServiceModels(serviceModels);
        flow.setQueueModels(queueModels);

    }
}
